﻿/**
 * Proyecto: Cognitum
 * Global IT Services 2008
 * @author
 */
Cognitum.Polizas.Documento.Editor = Ext.extend(Karma.EBCard, {

	operation: 'Service/DocumentoService.aspx?operation=save',
	
    fileUpload: true,
    
    sections: [{
        title: 'Datos generales',
        items: [{
            fieldLabel: 'Nombre',
            name: 'Nombre'
        }, {
            fieldLabel: 'Descripcion',
            name: 'Descripcion'
        }, {
            xtype: 'fileuploadfield',
            fieldLabel: 'Archivo',
            name: 'Valor',
			buttonCfg: {
                iconCls: 'upload-icon'
            }
        }]
    }, {
        xtype: 'aud.editor',
        title: 'Auditoria'
    }],
    
    initComponent: function(){
        Cognitum.Polizas.Documento.Editor.superclass.initComponent.apply(this, arguments);
    },
    
    doSubmit: function(){
        var value = this.getForm().getFieldValues();
        if (this.isValid() && this.validateValue(value)) {
            this.fireEvent('beforesave', value);
            this.fireEvent('any');
            
            var myin = document.createElement("input");
            myin.type = 'hidden';
            myin.id = 'json';
            myin.name = 'json';
            myin.value = Ext.encode(value);
            this.getForm().getEl().appendChild(myin);
            
            this.getForm().submit({
                url: this.operation,
                waitMsg: 'Procesando...',
                success: function(form, action){
                    var result = action.result;
                    if (result.Success === true) {
                        this.getBottomToolbar().setStatus({
                            text: 'El registro se guardo exitosamente',
                            iconCls: 'ok-icon',
                            clear: true
                        });
                        this.fireEvent('flush');
                    }
                    else {
                        Ext.Msg.show({
                            title: 'Error',
                            msg: 'Ocurrio un error: ' + result.ErrorMessage,
                            minWidth: 200,
                            modal: true,
                            icon: Ext.Msg.INFO,
                            buttons: Ext.Msg.OK
                        });
                    }
                }
.createDelegate(this)                ,
                errorReader: new Ext.data.JsonReader({
                    record: 'Data',
                    success: 'Success'
                }, ['id', 'msg']),
                failure: function(form, action){
                    var result = action.result;
                    if (result.Success == true) {
                        this.getBottomToolbar().setStatus({
                            text: 'El registro se guardo exitosamente',
                            iconCls: 'ok-icon',
                            clear: true
                        });
                        this.fireEvent('flush');
                    }
                    else {
                        Ext.Msg.show({
                            title: 'Error',
                            msg: 'Ocurrio un error: ' + result.ErrorMessage,
                            minWidth: 200,
                            modal: true,
                            icon: Ext.Msg.INFO,
                            buttons: Ext.Msg.OK
                        });
                    }
                }
.createDelegate(this)
            });
        }
    }
    
});

Ext.reg('doc.editor', Cognitum.Polizas.Documento.Editor);
